﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Data;

namespace Social_Celebrities
{
    public class ControlVisibilityVideo : IValueConverter
    {
        public ObservableCollection<ImageItems> urllist;
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Visibility visiblility = Visibility.Collapsed ;
            urllist = new ObservableCollection<ImageItems>();
            if (value == null) return null;
            MatchCollection matchevideos = Regex.Matches(value.ToString(), @"https?://(?:www.)?facebook.com/(?:video.php\?v=)?(.+)", RegexOptions.IgnoreCase);
            if (matchevideos.Count > 0)
            {
                string videoUrl = string.Empty;
                foreach (Match matchv in matchevideos)
               {
                   string url = matchv.Groups[0].Value;
                   videoUrl = url.Split('"')[0];
               }
                if (videoUrl.Contains("www.facebook.com/video.php"))
                {
                    visiblility = Visibility.Visible;
                }
                else
                    visiblility = Visibility.Collapsed;
            }
            else
            {
                GetHtmlImageUrlList(value.ToString());
                if (urllist.Count > 0)
                   visiblility= Visibility.Visible;
                else if (urllist.Count == 0)
                {
                    visiblility= Visibility.Collapsed;
                }
            }
            return visiblility;
        }
        public void GetHtmlImageUrlList(string sHtmlText)
        {

            Regex regImg = new Regex(@"]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
            MatchCollection matches = regImg.Matches(sHtmlText);
            int i = 0;
         
            foreach (Match match in matches)
            {
                urllist.Add(new ImageItems("img" + i, match.Groups["imgUrl"].Value, "", Visibility.Visible, Visibility.Collapsed));
                i++;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class ControlVisibility : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null) return null;

            //if (value.ToString().Contains("video.php") || value.ToString().Contains("http://youtu.be/"))
            //{

            //}

            List<ImageItem1> listUri = GetHtmlImageUrlList(value.ToString());
            if (listUri.Count > 0)
                return Visibility.Visible;
            else
                return Visibility.Collapsed;
        }



        public static List<ImageItem1> GetHtmlImageUrlList(string sHtmlText)
        {

            Regex regImg = new Regex(@"]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);



            MatchCollection matches = regImg.Matches(sHtmlText);
            int i = 0;
            List<ImageItem1> imgUrlList = new List<ImageItem1>();



            foreach (Match match in matches)
            {
                imgUrlList.Add(new ImageItem1("img" + i, match.Groups["imgUrl"].Value));
                i++;
            }
            return imgUrlList;
        }


        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class ImageItem1
    {
        public ImageItem1(string title, string url)
        {
            this.Title = title;
            this.URL = url;
        }


        public string Title { get; set; }
        public string URL { get; set; }
    }
}
